package pe.bn.WSServiceHost.serHost.model;

import java.util.Vector;
import pe.bn.WSServiceHost.serHost.interactive.WSData;

public class BodyCronogramaPago   extends WSData {
	
	public interface ICronograma {

		public void init();
		//public void InputData() throws Exception; 	
		public void FillBoby(String bodyOutPut) throws Exception;
		public String toString();
		public String getByIndex(int index);
		public String getByTag(String desc);
		public Campo getCampoByIndex(int index);
		public int LongitudTrama();
	}

	public class LCronograma extends Vector{
		  public ICronograma getCronograma(int i){
		  	return (ICronograma)get(i);
		  }
	}
	
	public class ItemCuota extends WSData
				implements ICronograma {
		
		public void init(){
			this.campos = new Campo[10];
			try{
				int num = -1;
				/*Ingreso ICronograma*/
				this.campos[++num]	= 	new Campo("NRO-CUOTA",	Campo.RETURN_INIT_BLNK,3);
				this.campos[++num]	= 	new Campo("FCH-VENCIM",	Campo.RETURN_INIT_BLNK,8);
				this.campos[++num]	= 	new Campo("SALDO",		Campo.RETURN_INIT_BLNK,17);
				this.campos[++num]	= 	new Campo("INTERES",	Campo.RETURN_INIT_BLNK,17);
				this.campos[++num]	= 	new Campo("SEG-DESGRAVAMEN",Campo.RETURN_INIT_BLNK,17);
				this.campos[++num]	= 	new Campo("SEG-INMUEBLE",	Campo.RETURN_INIT_BLNK,17);
				this.campos[++num]	= 	new Campo("PORTES",		Campo.RETURN_INIT_BLNK,17);
				this.campos[++num]	= 	new Campo("AMORTIZACION",Campo.RETURN_INIT_BLNK,17);
				this.campos[++num]	= 	new Campo("MNTO-CUOTA",	Campo.RETURN_INIT_BLNK,17);
				this.campos[++num]	= 	new Campo("CRN-AMORT-NEGA",	Campo.RETURN_INIT_BLNK,17);
				
			}catch (Exception e) {
				e.printStackTrace();
			}
		
		}
		
		public void InputData(
				String nroCuota
				,String fechaVenc
				,double saldo
				,double interes
				,double segDesgrav
				,double segInmeb
				,double portes
				,double amortz
				,double mntoCuota
			) throws Exception {

			init();
			int num = -1;
			boolean result = true; 
			result = ValidarCampo(true,this.campos[++num].setString(nroCuota));
			result = ValidarCampo(result,this.campos[++num].setString(fechaVenc));
			result = ValidarCampo(result,this.campos[++num].setDouble(saldo));
			result = ValidarCampo(result,this.campos[++num].setDouble(interes));
			result = ValidarCampo(result,this.campos[++num].setDouble(segDesgrav));
			result = ValidarCampo(result,this.campos[++num].setDouble(segInmeb));
			result = ValidarCampo(result,this.campos[++num].setDouble(portes));
			result = ValidarCampo(result,this.campos[++num].setDouble(amortz));
			result = ValidarCampo(result,this.campos[++num].setDouble(mntoCuota));
			
			if (!result){
				throw new Exception("Error al Dar Formato a la clase:" + this.getClass());
			}
			
		}
	
		public void FillBoby(String bodyOutPut) throws Exception {
			init();
			super.FillBoby(bodyOutPut);
		}
		
	}

	private LCronograma lcron = null;
	private int numeroCuotas = 30;
	
	public void initConsulta(){
		this.campos = new Campo[24];
		try {
			int num = -1;
			this.campos[++num]	= 	new Campo("NRO-EXPED",		Campo.NUM_CODIGO,12);
			this.campos[++num]	= 	new Campo("NRO-PAGARE",		Campo.NUM_CODIGO,14);
			this.campos[++num]	= 	new Campo("NRO-VERS",		Campo.NUM_CODIGO,2);
			this.campos[++num]	= 	new Campo("NRO-DESEMB",		Campo.NUM_CODIGO,2);
			this.campos[++num]	= 	new Campo("TIP-DOC-TIT",	Campo.NUM_CODIGO,2);
			this.campos[++num]	= 	new Campo("NRO-DOC-TIT",	Campo.CHAR_LEFT,TAMNO_DOC_IDENTIDAD);
			
			/*Informacion del Prestamo*/
			this.campos[++num]	= 	new Campo("COD-PRODUCTO",		Campo.NUM_CODIGO,6);
			this.campos[++num]	= 	new Campo("CENTRO-HIPOTECARIO",	Campo.NUM_CODIGO,6);
			this.campos[++num]	= 	new Campo("MONEDA",				Campo.NUM_CODIGO,2);
			
			this.campos[++num]	= 	new Campo("PLAZO-PAGO",			Campo.NUM_CODIGO,3);
			this.campos[++num]	= 	new Campo("PERIODO-GRACIA",		Campo.NUM_CODIGO,2);
			this.campos[++num]	= 	new Campo("CUOTA-JUL",			Campo.CHAR_LEFT,1);
			this.campos[++num]	= 	new Campo("CUOTA-DIC",			Campo.CHAR_LEFT,1);
			this.campos[++num]	= 	new Campo("TOT-FINANCIAR",		Campo.MTO_17,17); 
			this.campos[++num]	= 	new Campo("TASA-INTERES",		Campo.MTO_9,9); 
			this.campos[++num]	= 	new Campo("TIPO-DESGRAV",		Campo.NUM_CODIGO,1);
			this.campos[++num]	= 	new Campo("PORCENT-DESGRAV",	Campo.MTO_9,9); 
			this.campos[++num]	= 	new Campo("SEGURO-INMUEBLE",	Campo.MTO_9,9); 
			this.campos[++num]	= 	new Campo("PORTES",				Campo.MTO_17,17);
			this.campos[++num]	= 	new Campo("COMISION",			Campo.MTO_17,17);

			this.campos[++num]	= 	new Campo("VALOR-TASADO",		Campo.MTO_17,17);

			this.campos[++num]	= 	new Campo("PER-INICIAL",		Campo.NUM_CODIGO,3);
			this.campos[++num]	= 	new Campo("CANT-PER",			Campo.NUM_CODIGO,3);
			this.campos[++num]	= 	new Campo("COSTO-EFECT-ANUAL",	Campo.MTO_17,17);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void initCronograma(){
		
		try {
			initConsulta();
			this.lcron = new LCronograma();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	

	public void InputData(
			String nroExp
			,String nroPagare
			//,String tipoDocIdent
			//,String nroDocIdent
			//,String codProduct
			//,String centHipot
			//,String moneda
			,String plazoPago
			,String periodoGracia
			//,String cuotaJul
			//,String cuotaDic
			//,double totFinanciar
			//,double tasaInteres
			//,String tipoDesgv
			//,double porcDesgv
			//,double segInmub 
			//,double portes
			//,double comision
			//,double valorTasadInmub
			//,LCronograma lcron
			) throws Exception {
		
		initConsulta();
		int num = -1;
		boolean result = true;

		result = ValidarCampo(true,this.campos[++num].setString(nroExp));		
		result = ValidarCampo(result,this.campos[++num].setString(nroPagare));
		result = ValidarCampo(result,this.campos[++num].setString("01"));	//Nro Version
		result = ValidarCampo(result,this.campos[++num].setString("01"));	//Nro Desembolso
		result = ValidarCampo(result,this.campos[++num].setString("0"));
		result = ValidarCampo(result,this.campos[++num].setString(""));
		result = ValidarCampo(result,this.campos[++num].setString("0"));
		result = ValidarCampo(result,this.campos[++num].setString("0"));
		result = ValidarCampo(result,this.campos[++num].setString("0"));
		result = ValidarCampo(result,this.campos[++num].setString(plazoPago));
		result = ValidarCampo(result,this.campos[++num].setString(periodoGracia));
		result = ValidarCampo(result,this.campos[++num].setString("0"));
		result = ValidarCampo(result,this.campos[++num].setString("0"));
		result = ValidarCampo(result,this.campos[++num].setDouble(0));
		result = ValidarCampo(result,this.campos[++num].setDouble(0));
		result = ValidarCampo(result,this.campos[++num].setString("0"));
		result = ValidarCampo(result,this.campos[++num].setDouble(0));
		result = ValidarCampo(result,this.campos[++num].setDouble(0));
		result = ValidarCampo(result,this.campos[++num].setDouble(0));
		result = ValidarCampo(result,this.campos[++num].setDouble(0));
		result = ValidarCampo(result,this.campos[++num].setDouble(0));

		campos[++num].setString("0");
		campos[++num].setString("0");
		result = ValidarCampo(result,this.campos[++num].setDouble(0));
		
		//this.lcron = lcron;

		if (!result){
			throw new Exception("Error al Dar Formato a la clase:" + this.getClass());
		}
		
	}

	public int LongitudTramaList(){
		
		int longitudTrama = 0;
		int longCron = (new ItemCuota()).LongitudTrama();
		longitudTrama =  numeroCuotas*longCron;   //super.LongitudTrama()
		return longitudTrama;
	}

	public int LongitudTramaCuota(){
		
		int longCron = (new ItemCuota()).LongitudTrama();
		return longCron ;
	}
	
	public void FillBoby(String bodyOutPut) throws Exception {
	
		initCronograma();
		super.FillBoby(bodyOutPut);
		int longitudresult =bodyOutPut.length();
		int longicronog =super.LongitudTrama();
		
		int totalReg = numeroCuotas;
		int longCronograma = longicronog;
		int longBodyCron = (new ItemCuota()).LongitudTrama();
		
		for(int i=0;i<totalReg;i++) {
			
			if ( longitudresult >= longCronograma+longBodyCron){
				String BodyResult = bodyOutPut.substring(longCronograma
							,longCronograma+longBodyCron);
				ICronograma ICronograma = new ItemCuota();
				ICronograma.FillBoby(BodyResult);
				lcron.add(ICronograma);
			}else{
				ICronograma ICronograma = null;
				lcron.add(ICronograma);
			};
			longCronograma = longCronograma + longBodyCron;
		}
		
	}

	public String toString(){
		StringBuffer camposstring = new StringBuffer("");
		camposstring.append( super.toString() + separador);
		if(lcron != null){
			for(int i=0;i<lcron.size();i++) {
				camposstring.append( ((ICronograma)lcron.get(i)).toString() + separador);	
			}		
		}
		return camposstring.toString();
	}

	public LCronograma getLcron() {
		return lcron;
	}
	
	public int getNumeroCuotas() {
		return numeroCuotas;
	}
	public void setNumeroCuotas(int numeroCuotas) {
		this.numeroCuotas = numeroCuotas;
	}
	public void setLcron(LCronograma lcron) {
		this.lcron = lcron;
	}
}
